
PLUG.Platform = PLUG_PLATFORM_CLIENT

PLUG.ClassName = "cl_quickmenu"
PLUG.PrintName = "Quick Action Menu"
PLUG.Author = "thomasfn"

local QMENU

function PLUG:AddHooks()
	concommand.Add( "+g_quick", function() plugs.Call( "OpenQuickMenu" ) end )
	concommand.Add( "-g_quick", function() plugs.Call( "CloseQuickMenu" ) end )
end

local bgcol = Color( 255, 255, 255, 100 )
function PLUG:OpenQuickMenu()
	self:CloseQuickMenu()
	if (plugs.Call( "MenuOpen" )) then return end
	
	QMENU = vgui.Create( "DPanel" )
	QMENU:SetSize( ScrW()*0.15, ScrH()*0.8 )
	QMENU:SetPos( ScrW()*0.04, ScrH()*0.1 )
	function QMENU:Paint()
		draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
	end
	
	local plist = vgui.Create( "DComboBox", QMENU )
	plist:SetSize( QMENU:GetWide()-10, QMENU:GetTall()-10 )
	plist:Center()
	for _, ply in pairs( player.GetAll() ) do
		local item = plist:AddItem( ply:Nick() )
		item.ply = ply
	end
	
	plist.SelectItem2 = plist.SelectItem
	function plist:SelectItem( item, onlyme )
		local ply = item.ply
		if ((ply) && (ply:IsValid())) then
			plugs.Call( "QuickMenu_Player", ply )
		end
		self:SelectItem2( item, onlyme )
	end
	
	gui.EnableScreenClicker( true )
end

function PLUG:CloseQuickMenu()
	if (QMENU && QMENU:IsValid()) then QMENU:Remove() end
	QMENU = nil
	gui.EnableScreenClicker( false )
end

function PLUG:QuickMenu_Player( ply )
	local basic = plugs.MergeCall( "BuildPlayerActionList", "BASIC" )
	local punish = plugs.MergeCall( "BuildPlayerActionList", "PUNISH" )
	local misc = plugs.MergeCall( "BuildPlayerActionList", "MISC" )
	local menu = DermaMenu()
	menu.ply = ply
	self:AddToDMenu( menu, basic )
	menu:AddSpacer()
	self:AddToDMenu( menu, punish )
	menu:AddSpacer()
	self:AddToDMenu( menu, misc )
	menu:Open()
end

function PLUG:AddToDMenu( menu, options )
	for _, v in pairs( options ) do
		local id = v[1]
		local name = v[2]
		local priv = v[3]
		if (plugs.Call( "HasPriv", priv )) then
			local function Click()
				timer.Simple( 0.1, plugs.Call, "RunPlayerOperation", id, menu.ply )
			end
			menu:AddOption( name, Click )
		end
	end
end